package com.iwop.moto.controller.web;

import com.iwop.moto.comm.ErrCode;
import com.iwop.moto.controller.BaseController;
import com.iwop.moto.model.ResultModel;
import com.iwop.moto.model.SuperRecordModel;
import com.iwop.moto.model.User;
import com.iwop.moto.service.UserService;
import com.iwop.moto.utils.stringUtil.StringUtils;
import com.iwop.moto.utils.weixin.utils.CodeUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.image.BufferedImage;
import java.io.IOException;
import java.util.List;
import javax.imageio.ImageIO;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
/**
 * 生成随机图片，用来作为验证码
 */
@Controller
public class DrawImageController extends BaseController{
    private static final long serialVersionUID = 3038623696184546092L;
    public static final int WIDTH = 90;//生成的图片的宽度
    public static final int HEIGHT = 30;//生成的图片的高度

    @Autowired
    UserService userService;

    @RequestMapping("/api/code")
    @ResponseBody
    public void createCode(HttpServletRequest request,HttpServletResponse response) throws IOException {
        String createTypeFlag = request.getParameter("createTypeFlag");//接收客户端传递的createTypeFlag标识
        //1.在内存中创建一张图片
        BufferedImage bi = new BufferedImage(WIDTH, HEIGHT,BufferedImage.TYPE_INT_RGB);
        //2.得到图片
        Graphics g = bi.getGraphics();
        //3.设置图片的背影色
        CodeUtil.setBackGround(g);
        //4.设置图片的边框
        CodeUtil.setBorder(g);
        //5.在图片上画干扰线
        CodeUtil.drawRandomLine(g);
        //6.写在图片上随机数
        //String random = drawRandomNum((Graphics2D) g,"ch");//生成中文验证码图片
        //String random = drawRandomNum((Graphics2D) g,"nl");//生成数字和字母组合的验证码图片
        //String random = drawRandomNum((Graphics2D) g,"n");//生成纯数字的验证码图片
        //String random = drawRandomNum((Graphics2D) g,"l");//生成纯字母的验证码图片
        String random =  CodeUtil.drawRandomNum((Graphics2D) g, createTypeFlag);//根据客户端传递的createTypeFlag标识生成验证码图片

        //7.将随机数存在session中
        String checkcode = (String)request.getSession().getAttribute("checkcode");
        delValueFromSession(checkcode);
        request.getSession().setAttribute("checkcode", random);
        putValueFromSession(random, random,300);
        //8.设置响应头通知浏览器以图片的形式打开
        response.setContentType("image/jpeg");//等同于response.setHeader("Content-Type", "image/jpeg");
        //9.设置响应头控制浏览器不要缓存
        response.setDateHeader("expries", -1);
        response.setHeader("Cache-Control", "no-cache");
        response.setHeader("Pragma", "no-cache");
        //10.将图片写给浏览器
        ImageIO.write(bi, "jpg", response.getOutputStream());
    }

    /**
     * 根据手机号修改
     */
    @RequestMapping(value = "/api/user/updateAuditFlag")
    @ResponseBody
    public ResultModel updateUserAuditFlag() {
        SuperRecordModel SRModel = this.getSRModel();
        String  tel = (String) SRModel.get("tel");
        User user = new User();
        user.setAuditFlag("1");
        user.setTel(tel);
        user.setUserType("1");
        userService.updateUserByTel(user);
        return getSucceed(ErrCode.SUCCEED);
    }

}